/*
 * 
 * iFinity Software Development - http://www.ifinity.com.au
 * Copyright (c) 2006 - 2009
 
 * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated 
 * documentation files (the "Software"), the rights to use, copy, modify, merge, publish, distribute, sublicense
 * copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the 
 * following conditions:

 * 1. The above copyright notice and this permission notice shall be included in all copies or substantial portions 
 *    of the Software.  
 * 2. The software may not be claimed as the original work of anyone other than the author and may not be sold as 
 *    a stand alone product

 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED 
 * TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 
 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF 
 * CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 
 * DEALINGS IN THE SOFTWARE.
 * 
 */

using System;
using System.Collections.Generic;
using System.Text;


using System.Web.Configuration;
using System.Configuration.Provider;

using DotNetNuke.Framework.Providers;

namespace DotNetNuke.Providers.SearchEngineSiteMapProvider
{
    public class SearchEngineSitemapService
    {
        private static SiteMapProvider _provider = null;
        private static SearchEngineSitemapProviderCol _providers = null;
        private static object _lock = new object();
        private static bool _debug = false;
        internal static List<string> _providerLoadMessages = new List<string>();
        public static string GetGoogleSiteMap(string siteURL, bool debug)
        {
            //ensure providers are loaded
            _debug = debug;
            LoadSiteMapProviders(debug);
            //delegate to the default provider
            _provider.Debug = debug;
            return _provider.GetGoogleSiteMap(siteURL);
        }
        private static void LoadSiteMapProviders(bool debug)
        {
            if (_provider == null)
            {
                lock (_lock)
                {
                    //double check provider still null
                    if (_provider == null)
                    {
                        //get reference to GoogleSiteMaps section
                        //GoogleSiteMapSection section = (GoogleSiteMapSection)WebConfigurationManager.GetSection("dotnetnuke/searchEngineSitemap");

                        ProviderConfiguration pc = ProviderConfiguration.GetProviderConfiguration("searchEngineSitemap");
                        //load the registered providers and point _provider to default provider
                        _providers = new SearchEngineSitemapProviderCol();
                        
                        if (pc != null && pc.Providers != null)
                        {
                            foreach (string providerName in pc.Providers.Keys)
                            {
                                try
                                {
                                    Provider provider = (Provider)pc.Providers[providerName];
                                    Type providerType = Type.GetType(provider.Type);
                                    if (providerType != null)
                                    {
                                        //if type is valid, then instantiate the instance
                                        SiteMapProvider obj = (SiteMapProvider)Activator.CreateInstance(providerType, providerName, provider.Attributes);
                                        if (obj != null)
                                        {
                                            _providers.Add(obj);
                                        }
                                        if (providerName == pc.DefaultProvider)
                                            _provider = obj;
                                    }
                                    else
                                    {
                                        _providerLoadMessages.Add("Provider Type : " + providerName + "[ " + provider.Type + "] - no valid Type was found. Check the type declaration in the configuration file");
                                    }
                                }
                                catch (Exception ex)
                                {
                                    //something wrong with a provider
                                    
                                    _providerLoadMessages.Add("Exception Loading Provider " + providerName + ": " + ex.Message);
                                }
                            }
                        }


                        if (_provider == null)
                        {
                            throw new ProviderException("Unable to load default Search Engine Sitemap Provider");
                        }
                    }
                }
            }
        }
        static internal SearchEngineSitemapProviderCol Providers
        {
            get 
            {
                LoadSiteMapProviders(_debug);
                return _providers; 
            }
        }
    }
}
